<?php
class GameController extends EController
{
    public $layout = 'main';
    //过滤器
    public function filters()
    {
        return array(
            array(
                'application.filters.BackEndAuthRequestFilter',
            )
        );
    }

    public function actionlist()
    {
        $pid = $_GET['id'];
        $cookie = new CHttpCookie('aiuserhttpbor', str_replace(Yii::app()->request->baseUrl."/","",$_SERVER["REQUEST_URI"]));
        $cookie->expire = time()+60*60*24*30;
        Yii::app()->request->cookies['aiuserhttpbor']=$cookie;
        $this->layout='list';
        $this->son_menu=0;
        $act_list=ActionMenuHelper::getHiddenMenu();
        $model=new actGames('search');
        if(isset($_GET['actGames'])){
            $model->attributes=$_GET['actGames'];
        }
        $this->render('list',array(
                'dataProvider'=>$model->search(),
                'act_list'=>$act_list,
                'model'=>$model)
        );
    }

    public function actionAdd()
    {
        $this->layout='list';
        $this->son_menu=1;
        $model = new GamesAddForm;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if(isset($_POST['GamesAddForm']))
        {
            $model->attributes=$_POST['GamesAddForm'];
            // 验证用户输入，并在判断输入正确后重定向到
            if($model->validate())
            {
                if($model->add())
                {
                    $this->showMessage(Yii::t('info','operation success'),'aiunify/game/list');
                }
                else
                {
                    $this->showMessage(Yii::t('info','operation failed'),'aiunify/game/list');
                }
            }
        }
        $this->render('add',array('model'=>$model));
    }


    //编辑角色
    public function actionEdit()
    {

        $this->layout='pop';
        $admin_id = $this->_getId();
        $admin = actGames::model()->findByPk($admin_id);
        $model = new GamesEditForm();
        $model->attributes = $admin->attributes;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if(isset($_POST['GamesEditForm']))
        {
            $model->attributes=$_POST['GamesEditForm'];
            // 验证用户输入，并在判断输入正确后重定向到

            if($model->validate())
            {
                if($model->edit($admin_id))
                {
                    Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
                }
                else
                {
                    Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
                }
                $this->refresh();
            }
        }
        $this->render('edit',array('model'=>$model));
    }
    public function actiondelete(){
        $id = $this->_getId();
        $r = actGames::model()->findByPk($id);
        if ($r) {
            $r->del_flag  = 1;
            $r->save();
            $this->showMessage(Yii::t('info','operation success'));
        }
        $this->showMessage(Yii::t('info','operation failed'));

    }

    //获取id
    private function _getId(){
        if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
            throw new CHttpException(400,'Invalid request. id is required!');
        }
        return $_GET['id'];
    }
}